package in.iqing.base;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.iqing.app.R;
import in.iqing.base.internal.LayoutHolder;
import in.iqing.control.api.ApiHelper;

public abstract class BaseFragment extends Fragment {

	@Nullable
	@Bind(R.id.content)
	public FrameLayout customContainer;

	private LayoutHolder holder;

	protected final String TAG = getClass().getName();
	protected final String VOLLEY_TAG = TAG;

	public BaseFragment() {
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		onInitialize(savedInstanceState);
		onPostCreate(savedInstanceState);
	}

	protected void onInitialize(Bundle savedInstanceState) {
		View view = getView();
		ButterKnife.bind(getFragment(), view);

		if (customContainer != null) {
			this.holder = LayoutHolder.newInstance(customContainer);
		} else if (view instanceof FrameLayout) {
			this.holder = LayoutHolder.newInstance((FrameLayout) getView());
		}
	}

	protected void onPostCreate(Bundle savedInstanceState) {
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();

		ApiHelper.getSingleton().cancel(VOLLEY_TAG);
		ButterKnife.unbind(this);
	}

	protected Context getContext() {
		return getActivity().getApplicationContext();
	}

	protected Fragment getFragment() {
		return this;
	}

	protected void exit() {
		((BaseActivity) getActivity()).exit();
	}

	protected void showEmpty() {
		holder.showEmpty();
	}

	protected void showLoading() {
		holder.showLoading();
	}

	protected void showContent() {
		holder.showContent();
	}
}
